import java.util.List;
import java.util.Scanner;

public class DecoratorPatternModule {
    private final Scanner scanner = new Scanner(System.in);
    private final FactoryPatternModule factoryModule;
    
    public DecoratorPatternModule(FactoryPatternModule factoryModule) {
        this.factoryModule = factoryModule;
    }
    
    public void execute() {
        List<AgriculturalMachine> machines = factoryModule.getMachines();
        if (machines.isEmpty()) {
            System.out.println("没有可用的农机设备");
            return;
        }

        while (true) {
            System.out.println("\n===== 农机功能扩展 =====");
            System.out.println("1. 添加GPS功能");
            System.out.println("2. 查看农机状态");
            System.out.println("3. 返回主菜单");
            System.out.print("请选择操作(1-3): ");
            
            int choice = scanner.nextInt();
            scanner.nextLine();  // 消耗换行符
            
            switch (choice) {
                case 1 -> addGPSFeature(machines);
                case 2 -> viewMachineStatus(machines);
                case 3 -> { return; }
                default -> System.out.println("无效选择，请重新输入");
            }
        }
    }
    
    private void addGPSFeature(List<AgriculturalMachine> machines) {
        viewMachineStatus(machines);
        System.out.print("选择要添加GPS功能的农机编号: ");
        int index = scanner.nextInt() - 1;
        scanner.nextLine();  // 消耗换行符
        
        if (index < 0 || index >= machines.size()) {
            System.out.println("无效的农机编号");
            return;
        }
        
        AgriculturalMachine original = machines.get(index);
        AgriculturalMachine gpsMachine = new GPSDecorator(original);
        machines.set(index, gpsMachine);
        System.out.println("GPS功能已添加");
    }
    
    private void viewMachineStatus(List<AgriculturalMachine> machines) {
        System.out.println("\n=== 农机状态 ===");
        for (int i = 0; i < machines.size(); i++) {
            System.out.println((i+1) + ". " + machines.get(i).getStatus());
        }
    }
}